package com.atguigu.controller;

import com.atguigu.entity.SkuInfo;
import com.atguigu.mapper.SkuInfoMapper;
import org.redisson.api.RBloomFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/init")
public class BloomController {

    @Autowired
    private SkuInfoMapper skuInfoMapper;
    @Autowired
    private RBloomFilter<Long> skuBloomFilter;

    /**
     * TODO 后期定时任务去进行定时初始化
     * 把数据库中sku的id放到布隆过滤器中
     * @return
     */
    @GetMapping("/sku/bloom")
    public String initSkuBloom() {
        //重新初始化 把原来的删除掉
        skuBloomFilter.delete();
        skuBloomFilter.tryInit(10000000, 0.001);

        //a.查询指定表的数据库
        List<SkuInfo> skuInfoList = skuInfoMapper.selectList(null);
        for (SkuInfo skuInfo : skuInfoList) {
            Long skuInfoId = skuInfo.getId();
            skuBloomFilter.add(skuInfoId);
        }
        return "布隆过滤器初始化完成";
    }
}
